package cn.kys.modules.system.controller;

import cn.kys.logging.aop.log.EnableKysLog;
import cn.kys.modules.system.service.CaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/case_api")
public class CaseApi {
    @Autowired
    private CaseService caseService;

    @GetMapping("/getAllCases")
    public Map getAllCases() {
        Map returnMap = new HashMap();
        try {
            List list = caseService.getAllCases();
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //添加方案
    @GetMapping("/addCase")
    public Map addCase(@RequestParam("name") String name, @RequestParam("is_public") boolean is_public) {
        Map returnMap = new HashMap();
        try {
            Map map = caseService.addCase(name, is_public);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @GetMapping("/getCasesByParam")
    public Map getCasesByParam(@RequestParam("is_public") boolean is_public) {
        Map returnMap = new HashMap();
        try {
            List list = caseService.getCasesByParam(is_public);
            returnMap.put("data", list);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //编辑表单
    @GetMapping("/editCase")
    public Map editCase(@RequestParam("id") String id, @RequestParam("name") String name, @RequestParam("caseformids") String caseformids
            , @RequestParam("casebaselayerids") String casebaselayerids
            , @RequestParam("casespeciallayerids") String casespeciallayerids
            , @RequestParam("caseeditlayerid") String caseeditlayerid) {
        Map returnMap = new HashMap();
        try {
            Map map = caseService.editCase(id, name, caseformids, casebaselayerids, casespeciallayerids, caseeditlayerid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //获取已经绑定的formcontent
    @GetMapping("/getBindingByCaseId")
    public Map getBindingByCaseId(@RequestParam("id") String id) {
        Map returnMap = new HashMap();
        try {
            Map map = caseService.getBindingByCaseId(id);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    //获取底图、专题图、工作图
    @GetMapping("/getLayersByCaseId")
    public Map getLayersByCaseId(@RequestParam("caseid") String caseid) {
        Map returnMap = new HashMap();
        try {
            Map map = caseService.getLayersByCaseId(caseid);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @GetMapping("/delCaseById")
    public Map delCaseById(@RequestParam("id") String id) {
        Map returnMap = new HashMap();
        try {
            Map map = caseService.delCaseById(id);
            returnMap.put("data", map);
            returnMap.put("success", true);
            returnMap.put("resMsg", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success", false);
            returnMap.put("resMsg", "异常");
        }
        return returnMap;
    }

    @EnableKysLog("返回未分配的shp表给前台")
    @ApiOperation("返回未分配的shp表给前台")
    @GetMapping(value = "/queryTableDeptUnDistribute")
    public ResponseEntity<Object> queryTableDeptUnDistribute() {
        List list = caseService.queryTableDeptUnDistribute();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @EnableKysLog("查询TableDept表")
    @ApiOperation("查询TableDept表")
    @GetMapping(value = "/queryTableDept")
    public ResponseEntity<Object> queryTableDept() {
        List list = caseService.queryTableDept();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

   /* @EnableKysLog("新增TableDept表")
    @ApiOperation("新增TableDept表")
    @GetMapping(value = "/insertTableDept")
    public Map insertTableDept(@RequestParam("tablename") String tablename) {
        Map returnMap = new HashMap();
        try {
            boolean is = caseService.insertTableDept(tablename);
            returnMap.put("data",is);
            returnMap.put("success",true);
            returnMap.put("resMsg",tablename+"表含geom字段，且geom下有值，该表成功绑定到t_table_dept");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg",tablename+"表不含geom字段，失败绑定到t_table_dept");
        }
        return returnMap;
    }*/

    @EnableKysLog("删除TableDept表")
    @ApiOperation("删除TableDept表")
    @GetMapping(value = "/delTableDept")
    public ResponseEntity<Object> delTableDept(@RequestParam("id") String id) {
        caseService.delTableDept(id);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @EnableKysLog("编辑TableDept表")
    @ApiOperation("编辑TableDept表")
    @GetMapping(value = "/editTableDept")
    public ResponseEntity<Object> editTableDept(@RequestParam("tablename") String tablename) {
        caseService.editTableDept(tablename);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }


}

